/**
 * Created by Administrator on 14-3-18.
 */
$(window).load(function(){
  //公共脚本区
  var sideMenu = $('#sideMenu')
    , top = $('#top')
    , topHeight = top.height()
    , main = $('#main')
    , clientHeight = $(window).height()
    , clientWidth = $(window).width()
    , setLayout = function(){ //设置布局
      clientHeight = $(window).height();
      clientWidth = $(window).width();
      sideMenu.height(clientHeight-topHeight);
      sideMenu.find('.item')
        .filter(function(){
          if (!$(this).hasClass('unfolded')) return $(this);
        })
        .mouseenter(function(){
          $(this).addClass('hover');
          $(this).find('.child').show();
        })
        .mouseleave(function(){
          $(this).removeClass('hover');
          $(this).find('.child').hide();
        });
      main.css({
        minHeight : clientHeight-topHeight,
        width : clientWidth - sideMenu.width()
      });
    };
  setLayout();
  $(window).resize(function(){setLayout()});
  //全选
  $('[check_all]').click(function(){
    var checkTarget = $(this).attr('check_all')
      , targetLen = $(checkTarget+':checked').length
      , self = this;
    if(targetLen)
      $(checkTarget).attr('checked', false);
    else
      $(checkTarget).attr('checked', true);
    $(checkTarget).click(function(){
      if ($(checkTarget+':checked').length)
        $(self).attr('checked', true);
      else
        $(self).attr('checked', false)
    });
  });
  //快捷键
  $(this).bind('keydown', function(e){
    if(e.keyCode == 83 && e.ctrlKey) {
      $('form').submit();
      return false;
    }
  });
});

/**
 * KindEditor配置
 */

KindEditor.ready(function(K){
  K.create('[editor_field]', {
    afterCreate : function() {
      var self = this;
      K.ctrl(this.edit.iframe.get(0).contentWindow.document, 13, function() {
        self.sync();
        $('#enterResponse').trigger('click');
      });
    },
    afterBlur : function() {
      this.sync();
    },
    minWidth : 700,
    minHeight : 400,
    uploadJson : '/upload.js',
    fileManagerJson : '/filemanager.js',
    allowFileManager : true
  });

});

/**
 * jQuery插件
 */
(function($){
  //ajax提交表单
  $.fn.ajaxSubmit = function(options){
    options = options || {};
    return this.each(function(){
      var form = $(this)
        , action = form.attr('action') || location.href
        , submitValue = form.find('input[type=submit]').val();

      if (this.nodeName != 'FORM') return;
      form.bind('submit', function(){
        form.find('input[type="submit"]').val('请稍等...').attr('disabled', 'disabled');
        $.post(action, form.serializeArray(), function(res){
          if (res.success) {
            if (typeof options.callback == 'function') options.callback(res);
            if (options.redirect) location.href = options.redirect;
          } else {
            alert(res.msg);
          }
          form.find('input[type="submit"]').val(submitValue).removeAttr('disabled');
        }, 'json');
        return false;
      });
    });
  };
})(jQuery);